package cn.tedu._06article.base.exception;

import cn.tedu._06article.base.response.JsonResult;
import cn.tedu._06article.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(IllegalArgumentException.class)
    public JsonResult doHandleIllegalArgumentException(IllegalArgumentException ex) {
        log.debug("参数异常信息："+ex.getMessage());
        String data = ex.getMessage();
        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }
    @ExceptionHandler(RuntimeException.class)
    public JsonResult doHandleRuntimeException(RuntimeException ex) {
        log.debug("运行时异常信息："+ex.getMessage());
        String data = ex.getMessage();
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        log.debug("参数验证异常信息："+ex.getMessage());
        String data = ex.getBindingResult().getFieldError().getDefaultMessage();
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }
}
